package com.xebest.util;


import com.xebest.constant.XeConstans;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class DateUtil {
    private static final String PATTEN_SECONDS = "yyyy-MM-dd HH:mm:ss";
    private static final String PATTEN_MONTH = "yyyy-MM-dd";

    private DateUtil() {
    }

    /**
     * 获取时间 形式为yyMMddHHmmssSSS
     */
    public static String toString(Date date) {
        SimpleDateFormat df = new SimpleDateFormat(PATTEN_SECONDS);
        return df.format(date);
    }

    /**
     * String转date（精确到秒）
     *
     * @author你找我你找我吧逗他 李书峰
     */
    public static Date toDate(String str) {
        SimpleDateFormat format = new SimpleDateFormat(PATTEN_SECONDS);
        Date date = null;
        try {
            date = format.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * String转date（精确到日）
     *
     * @author 李书峰
     */
    public static Date toDay(String str) {
        SimpleDateFormat format = new SimpleDateFormat(PATTEN_MONTH);
        Date date = null;
        try {
            date = format.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 获取时间 形式为yyMMdd
     */
    public static String getDay(Date date) {
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
        return df.format(date);
    }

    public static String getDate(Date date) {
        SimpleDateFormat df = new SimpleDateFormat(PATTEN_SECONDS);
        return df.format(date);
    }


    /**
     * 根据传入的日期格式，获取最近一周的时间周期
     *
     * @author Duchunjing
     */
    public static Map<String, String> getLatelySevenDate() {
        //Date dateNow = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar dateNow = Calendar.getInstance();
        dateNow.set(Calendar.HOUR_OF_DAY, 23);
        dateNow.set(Calendar.MINUTE, 59);
        dateNow.set(Calendar.SECOND, 59);

        Calendar dateFrom = Calendar.getInstance();
        dateFrom.setTime(dateNow.getTime());
        dateFrom.add(Calendar.WEEK_OF_YEAR, -1);
        dateFrom.set(Calendar.HOUR_OF_DAY, 00);
        dateFrom.set(Calendar.MINUTE, 00);
        dateFrom.set(Calendar.SECOND, 00);

        System.out.println(sdf.format(dateFrom.getTime()) + "到" + sdf.format(dateNow.getTime()));
        Map<String, String> map = new HashMap<String, String>();
        map.put("dateFrom", sdf.format(dateFrom.getTime()));
        map.put("dateNow", sdf.format(dateNow.getTime()));
        return map;
    }


    /**
     * 获取默认日期 yyyy-MM-dd 00:00:00
     *
     * @author LBB
     */
    public static String getDefaultDate() {
        SimpleDateFormat df = new SimpleDateFormat(PATTEN_MONTH);
        Calendar cal_1 = Calendar.getInstance();// 获取当前日期
        cal_1.add(Calendar.DATE, 0);
        String sta = df.format(cal_1.getTime()) + " 00:00:00";
        return sta;
    }

    /**
     * 根据传入天数获取日期， 默认 获取当天日期 yyyy-MM-dd 00:00:00
     *
     * @param days 添加的天数或时间  0为当天
     * @author LBB
     */
    public static String getDefaultDate(int days) {
        SimpleDateFormat df = new SimpleDateFormat(PATTEN_MONTH);
        Calendar cal_1 = Calendar.getInstance();// 获取当前日期
        cal_1.add(Calendar.DATE, days);
        String sta = df.format(cal_1.getTime()) + " 00:00:00";
        return sta;
    }

    /**
     * 获取日期的最后时间  如：2016-05-12 23:59:59
     *
     * @param days 添加的天数或时间 0为当天
     * @author LBB
     */
    public static String getDayLastTime(int days) {
        SimpleDateFormat df = new SimpleDateFormat(PATTEN_MONTH);
        Calendar cal_1 = Calendar.getInstance();// 获取当前日期
        cal_1.add(Calendar.DATE, days);
        String sta = df.format(cal_1.getTime()) + " 23:59:59";
        return sta;
    }

    /**
     * 获取当月第一天
     *
     * @author 李书峰
     */
    public static String getMonthFirstDay() {
        SimpleDateFormat df = new SimpleDateFormat(PATTEN_SECONDS);
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.set(Calendar.DAY_OF_MONTH, 1);
        String firstDay = df.format(c.getTime());
        return firstDay;
    }

    /**
     * 获取当前月第一天
     *
     * @author LBB
     */
    public static String getFirestDay(String patten) {
        SimpleDateFormat df = new SimpleDateFormat(patten);
        //获取当前月第一天：
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH, 0);
        c.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天
        return df.format(c.getTime());
    }

    /**
     * 获取一周前日期
     *
     * @param patten 年月日
     */
    public static String getLastWeek(String patten) {
        SimpleDateFormat df = new SimpleDateFormat(patten);
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.WEEK_OF_YEAR, -1);
        return df.format(calendar.getTime());
    }


    /**
     * 根据传入数，获取前n个月的日期
     *
     * @author zpf </P>
     */
    public static String getFirstMonth(int n) {

        String paramStartDate = "";
        SimpleDateFormat sdf = new SimpleDateFormat(PATTEN_SECONDS);

        Date dateNow = new Date();
        Date dateBefore = new Date();

        Calendar cal = Calendar.getInstance();
        cal.setTime(dateNow);
        cal.add(Calendar.MONTH, -n);
        dateBefore = cal.getTime();
        paramStartDate = sdf.format(dateBefore);

        return paramStartDate;
    }

    /**
     * 获取n月前的日期，格式为yyyy-MM-dd
     *
     * @author 杜春静
     */
    public static String getFrontMonth(int n) {
        String paramStartDate = "";
        SimpleDateFormat sdf = new SimpleDateFormat(PATTEN_MONTH);

        Date dateNow = new Date();
        Date dateBefore = new Date();

        Calendar cal = Calendar.getInstance();
        cal.setTime(dateNow);
        cal.add(Calendar.MONTH, -n);
        dateBefore = cal.getTime();
        paramStartDate = sdf.format(dateBefore);

        return paramStartDate;
    }

    /**
     * 获取时间是否大于三个月
     */
    public static boolean comparableTime(String startTime, String endTime) {
        SimpleDateFormat sdf = new SimpleDateFormat(PATTEN_MONTH);
        try {
            Date start = sdf.parse(startTime);
            Date end = sdf.parse(endTime);
            long day = (end.getTime() - start.getTime()) / (24 * 60 * 60 * 1000);
            return day > 93 ? false : true;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return true;
    }

    /**
     * 根据日期字符串所在的日期，获取n个月前的日期字符串
     *
     * @author 杜春静  yyyy-MM-dd HH:mm:ss
     */
    public static String strToNMonth(String str, int n) {
        String paramStartDate = "";
        SimpleDateFormat sdf = null;
        if (str.trim().length() > XeConstans._XE_TEN) {
            sdf = new SimpleDateFormat(PATTEN_SECONDS);
        } else {
            sdf = new SimpleDateFormat(PATTEN_MONTH);
        }

        Date dateNow;
        try {
            dateNow = sdf.parse(str);

            Date dateBefore = new Date();
            Calendar cal = Calendar.getInstance();
            cal.setTime(dateNow);
            cal.add(Calendar.MONTH, n);
            dateBefore = cal.getTime();
            paramStartDate = sdf.format(dateBefore);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return paramStartDate;
    }

    /**
     * 根据月份获取月份最后一天
     *
     * @author 李贝贝
     */
    public static String getLastDay(int month) {
        SimpleDateFormat df = new SimpleDateFormat(PATTEN_MONTH);
        //获取当前月第一天：
        Calendar c = Calendar.getInstance();
        c.set(Calendar.MONTH, month - 1);
        int maxday = c.getActualMaximum(Calendar.DAY_OF_MONTH);
        c.set(Calendar.DAY_OF_MONTH, maxday);
        return df.format(c.getTime()) + " 23:59:59";
    }

    /**
     * 根据月份获取月份第一天
     *
     * @author 李贝贝
     */
    public static String getFirtDay(int month) {
        SimpleDateFormat df = new SimpleDateFormat(PATTEN_MONTH);
        //获取当前月第一天：
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.MONTH, month - 1);
        int minday = calendar.getActualMinimum(Calendar.DAY_OF_MONTH);
        calendar.set(Calendar.DAY_OF_MONTH, minday);
        return df.format(calendar.getTime()) + " 00:00:00";
    }

    /**
     * 毫秒转 21小时16分51秒
     *
     * @author 赵盼飞
     */
    public static String formatMillisecondTime(Long ms) {
        Integer ss = 1000;
        Integer mi = ss * 60;
        Integer hh = mi * 60;
        Integer dd = hh * 24;

        Long day = ms / dd;
        Long hour = (ms - day * dd) / hh;
        Long minute = (ms - day * dd - hour * hh) / mi;
        Long second = (ms - day * dd - hour * hh - minute * mi) / ss;
        Long milliSecond = ms - day * dd - hour * hh - minute * mi - second * ss;

        StringBuffer sb = new StringBuffer();
        if (day > 0) {
            sb.append(day + "天");
        }
        if (hour > 0) {
            sb.append(hour + "小时");
        }
        if (minute > 0) {
            sb.append(minute + "分");
        }
        if (second > 0) {
            sb.append(second + "秒");
        }
        if (milliSecond > 0) {
            sb.append(milliSecond + "毫秒");
        }
        return sb.toString();
    }

    /**
     * 得到指定月的天数
     */
    public static int getMonthLastDay(int year, int month) {
        Calendar a = Calendar.getInstance();
        a.set(Calendar.YEAR, year);
        a.set(Calendar.MONTH, month - 1);
        //把日期设置为当月第一天
        a.set(Calendar.DATE, 1);
        //日期回滚一天，也就是最后一天
        a.roll(Calendar.DATE, -1);
        int maxDate = a.get(Calendar.DATE);
        return maxDate;
    }

    /**
     * 获取日期所在周 周一的日期
     *
     * @author chenjing
     */
    public static String getFirstDayOfWeek(String date) throws Exception {
        SimpleDateFormat df = new SimpleDateFormat(PATTEN_MONTH);
        Calendar cal = Calendar.getInstance();
        if (date != null)
            cal.setTime(df.parse(date));
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        return df.format(cal.getTime()) + " 00:00:00";
    }

    /**
     * 返回给定日期加上多少天之后的时间<BR>
     *
     * @author chenjing
     */
    public static String getDateAdd(String from, int day, int lenth) {
        SimpleDateFormat sdf = new SimpleDateFormat(PATTEN_MONTH);
        Calendar calendar = Calendar.getInstance();
        try {
            calendar.setTime(sdf.parse(from.toString()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        calendar.add(Calendar.DAY_OF_MONTH, day);
        String date = sdf.format(calendar.getTime());
        if (14 == lenth) {
            date = sdf.format(calendar.getTime()) + " 00:00:00";
        }
        return date;
    }
}
